package com.huibo.shopping.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class BaseConfig {

	@Bean
	public Docket getDocket() {
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(getApi());
	}
	
	public ApiInfo getApi() {
		return  new ApiInfoBuilder()
				.title("分布式shop系统")
				.description("shop系统接口文档说明")
				.termsOfServiceUrl("http://localhost:9001")
				.contact(new Contact("keke","","1019510181@qq.com"))
				.version("1.0")
				.build();
	}
	
	
	@Bean
	public CorsFilter corsFilter() {
		CorsConfiguration cf = new CorsConfiguration();
		cf.addAllowedOrigin("http://localhost:8080");
		cf.addAllowedOrigin("http://127.0.0.1:8080");
		
		//是否发送Cookie信息
        cf.setAllowCredentials(true);
        //允许的请求方式
        cf.addAllowedMethod("OPTIONS");
        cf.addAllowedMethod("HEAD");
        cf.addAllowedMethod("GET");
        cf.addAllowedMethod("PUT");
        cf.addAllowedMethod("POST");
        cf.addAllowedMethod("DELETE");
        cf.addAllowedMethod("PATCH");
        cf.addAllowedHeader("*");
        UrlBasedCorsConfigurationSource ubcc = new UrlBasedCorsConfigurationSource();
        ubcc.registerCorsConfiguration("/**", cf);
        CorsFilter corsFilter = new CorsFilter(ubcc);
        return corsFilter;
	}
}
